' 
' Copyright (C) 2014 Infinity CCS Ltd (http://www.infinityccs.com/)
' Copyright (C) 2008 Sasa Coh <sasacoh@gmail.com>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program.  If not, see <http://www.gnu.org/licenses/>. 
'/


Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Infinity.pjsipNET.Common.Media

Namespace Common.CallState

#Region "IState"
  ''' <summary>
  ''' CAbstractState implements ICallProxyInterface interface. 
  ''' Sends requests to call server. 
  ''' It's a base for all call states classes (IStateMachine). 
  ''' </summary>
  Friend MustInherit Class IState
    Inherits ICallProxy

#Region "Properties"
    ''' <summary>
    ''' State identification property
    ''' </summary>
    Public Property Id As EStateId
      Get
        Return _stateId
      End Get
      Set(value As EStateId)
        _stateId = value
      End Set
    End Property

    ''' <summary>
    ''' Signaling proxy instance for communication with VoIP stack
    ''' </summary>
    Public ReadOnly Property CallProxy As ICallProxy
      Get
        Return _smref.CallProxy
      End Get
    End Property

    ''' <summary>
    ''' Call/Session identification
    ''' </summary>
    Public Overrides Property SessionId As Integer
      Get
        Return _smref.SessionId
      End Get
      Set(value As Integer)

      End Set
    End Property

    ''' <summary>
    ''' Media proxy instance for handling tones
    ''' </summary>
    Public ReadOnly Property MediaProxy As IMediaProxy
      Get
        Return _smref.MediaProxy
      End Get
    End Property

    Public Overrides Property AccountId As Integer
      Get
        Return _smref.AccountId
      End Get
      Set(value As Integer)

      End Set
    End Property

    Public Overrides Function ToString() As String
      Return _stateId.ToString()
    End Function
#End Region

#Region "Variables"

    Private _stateId As EStateId = EStateId.IDLE

    Protected _smref As IStateMachine

#End Region

#Region "Constructor"
    ''' <summary>
    ''' Abstract state construction.
    ''' </summary>
    ''' <param name="sm">reference to call state machine</param>
    Public Sub New(sm As CallState.IStateMachine)
      _smref = sm
    End Sub

#End Region

#Region "Abstract Methods"

    ''' <summary>
    ''' State entry method
    ''' </summary>
    Friend MustOverride Sub OnEntry()

    ''' <summary>
    ''' State exit method
    ''' </summary>
    Friend MustOverride Sub OnExit()

    ''' <summary>
    ''' Handle Release timer timeout. Release timer is usualy started when other 
    ''' party releases the call.
    ''' </summary>
    ''' <param name="sessionId"></param>
    ''' <returns></returns>
    Public Overridable Function releasedTimerExpired(sessionId As Integer) As Boolean
      Return False
    End Function

    ''' <summary>
    ''' Handler no response on incoming call. Trigerred by ENOANSWER timer
    ''' </summary>
    ''' <param name="sessionId"></param>
    ''' <returns></returns>
    Public Overridable Function noAnswerTimerExpired(sessionId As Integer) As Boolean
      Return False
    End Function

    ''' <summary>
    ''' Handler no response on incoming call. Trigerred by ENORESPONSE timer
    ''' </summary>
    ''' <param name="sessionId"></param>
    ''' <returns></returns>
    Public Overridable Function noResponseTimerExpired(sessionId As Integer) As Boolean
      Return False
    End Function

#End Region

#Region "Inherited methods"

    Public Overrides Function makeCall(dialedNo As String, accountKey As String) As Integer
      Return -1
    End Function

    Public Overrides Function endCall() As Boolean
      Return True
    End Function

    Public Overrides Function acceptCall() As Boolean
      Return True
    End Function

    Public Overrides Function alerted() As Boolean
      Return True
    End Function

    Public Overrides Function holdCall() As Boolean
      Return True
    End Function

    Public Overrides Function retrieveCall() As Boolean
      Return True
    End Function

    Public Overrides Function xferCall(number As String) As Boolean
      Return True
    End Function

    Public Overrides Function xferCallSession(partnersession As Integer) As Boolean
      Return True
    End Function

    Public Overrides Function threePtyCall(partnersession As Integer) As Boolean
      Return True
    End Function

    Public Overrides Function serviceRequest(code As Integer, dest As String) As Boolean
      CallProxy.serviceRequest(code, dest)
      Return True
    End Function

    Public Overrides Function dialDtmf(digits As String, mode As EDtmfMode) As Boolean
      CallProxy.dialDtmf(digits, mode)
      Return True
    End Function

    Public Overrides Function getCurrentCodec() As String
      '// not used!
      Return ""
    End Function

    Public Overrides Function conferenceCall() As Boolean
      Return False
    End Function

    'Public Overrides Function sendCallMessage(message As String) As Boolean
    '  Return CallProxy.sendCallMessage(message)
    'End Function
#End Region

#Region "Callbacks"

    Public Overridable Sub incomingCall(cli As String, ddi As String, clidisplay As String)
    End Sub

    Public Overridable Sub onAlerting()
    End Sub

    Public Overridable Sub onConnect()
    End Sub

    Public Overridable Sub onReleased()
    End Sub
    Public Overridable Sub onHoldConfirm()

    End Sub
#End Region

#Region "Null pattern"
    Friend Class NullState
      Inherits IState

      Public Sub New()
        MyBase.New(New NullStateMachine)
      End Sub

      Friend Overrides Sub OnEntry()

      End Sub

      Friend Overrides Sub OnExit()

      End Sub
    End Class
#End Region
  End Class
#End Region

End Namespace